package com.huabo.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.oracle.entity.TblAssesslevel;
import com.huabo.monitor.oracle.mapper.TblAssesslevelMapper;
import com.huabo.monitor.service.TblAssesslevelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service("TblAssesslevelService")
public class TblAssesslevelServiceImpl implements TblAssesslevelService {

    @Resource
    private TblAssesslevelMapper tblAssesslevelMapper;

    @Override
    public JsonBean findByPageBean(Integer pageNumber, Integer pageSize) {
        if (pageNumber == null || pageNumber <= 0){
            pageNumber = 1;
        }
        if (pageSize <= 0 || pageSize >15){
            pageSize = 15;
        }
        Page<TblAssesslevel> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNumber);
        LambdaQueryWrapper<TblAssesslevel> wrapper = Wrappers.<TblAssesslevel>lambdaQuery();
        Page<TblAssesslevel> tblAssesslevelPage = tblAssesslevelMapper.selectPage(page, wrapper);

        return ResponseFormat.retParam(1, 200, tblAssesslevelPage);
    }

    @Override
    public JsonBean findById(BigDecimal id) {
        TblAssesslevel tblAssesslevel = tblAssesslevelMapper.selectById(id);
        if (null == tblAssesslevel){
            return ResponseFormat.retParam(0,50001,"未找到该数据");
        }
        return ResponseFormat.retParam(1, 200, tblAssesslevel);
    }

    @Override
    public JsonBean add(TblAssesslevel tblAssesslevel) {
        tblAssesslevelMapper.insert(tblAssesslevel);
        return ResponseFormat.retParam(1, 200, "success");
    }

    @Override
    public JsonBean update(TblAssesslevel tblAssesslevel) {
        if (null == tblAssesslevel){
            return ResponseFormat.retParam(0, 10002, "参数为空，请检查后重试");
        }
        BigDecimal asslevid = tblAssesslevel.getAsslevid();
        TblAssesslevel dbAssesslevel = tblAssesslevelMapper.selectById(asslevid);
        if (null == dbAssesslevel){
            return ResponseFormat.retParam(0, 50001, "跟新数据 不存在，请检查后重试");
        }
        tblAssesslevelMapper.updateById(tblAssesslevel);
        return ResponseFormat.retParam(1, 200, "success");
    }

    @Override
    public JsonBean delete(BigDecimal asslevid) {
        if (null == asslevid){
            return ResponseFormat.retParam(0, 10002, "缺少参数，请检查重试");
        }
        TblAssesslevel dbAssesslevel = tblAssesslevelMapper.selectById(asslevid);
        if (null == dbAssesslevel){
            return ResponseFormat.retParam(0, 50001, "要删除的数据不存在");
        }
        tblAssesslevelMapper.deleteById(asslevid);
        return ResponseFormat.retParam(1, 200, "success");
    }
}
